<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo19</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            box-sizing: 0px;
        }

        .nav {
            width: 800px;
            height: 100px;
            align-items: center;
            margin: 0px auto;
            display: flex;
            
        }

        .nav input {
            width: 600px;
            height: 50px;
            font-size: 30px;
        }

        .nav button {
            width: 200px;
            height: 55px;
            border: none;
            border-radius: 5px;
            background-color: rgb(255, 149, 0);
            color: white;
        }

        .nav button:active {
            background-color: gray;
        }

        .container {
            width: 800px;
            margin: 0px auto;
            display: flex;
            
        }

        .todo, .done {
            width: 50%;
            height: 100%;
        }

        .container h3 {
            text-align: center;
            line-height: 50px;
            
            color: white;
            background-color: black;
            height: 55px;
        }

        .row {
            height: 50px;
            display: flex;
            align-items: center;
        }

        .row input {
           margin: 0 auto;
        }

        .row span {
            width: 300px;
        }

        .row button {
            width: 50px;
            height: 40px;
        }


    </style>
</head>
<body>
    <div class="nav">
        <input type="text"> <button onclick="createWork()">新建任务</button>
    </div>
    <div class="container">
        <div class="todo">
            <h3>未完成</h3>
            <div class="row">
                <input type="checkbox"> <span>吃饭</span> <button >删除</button>
            </div>
        </div>
        <div class="done">
            <h3>已完成</h3>
        </div>
    </div>
</body>
<script>

    function createWork() {
        //获取文本输入框的内容
        let input_element=document.querySelector('.nav input');
        let taskMessage=input_element.value;
        if (taskMessage=="") return;
        //创建每个标签的节点，再按照上述节点的顺序进行添加
        let div_node=document.createElement('div');
        let input_node=document.createElement('input');
        let button_node=document.createElement('button');
        let span_node=document.createElement('span')
        div_node.className='row'
        input_node.type='checkbox';
        span_node.innerHTML=taskMessage;
        button_node.innerHTML='删除';
        div_node.appendChild(input_node);
        
        div_node.appendChild(span_node);
        div_node.appendChild(button_node);

        //在将创建好的节点加到对应的位置上
        let todo_element=document.querySelector('.container .todo');
        todo_element.appendChild(div_node);


        //删除节点进行操作，之所以是在新建任务的函数中进行删除是因为，任务新建后
        //任务的个数会发生改变，此时可以直接在新建任务函数中获取全部任务的节点，会比较方便

        let delete_elements = document.querySelectorAll('.row button');
        for (let i = 0; i < delete_elements.length; i++) {
            delete_elements[i].onclick=function () {
                let parentnode=this.parentNode;//this是当前button按钮节点,他的父节点是row
                let grandnode=parentnode.parentNode;//row的父节点是todo节点
                //在todo节点删除节点
                grandnode.removeChild(parentnode);
            }
        }

        //点击复选框,完成任务
        let done_elements = document.querySelectorAll('.row input');
        let target;//确定任务是否完成的状态表示
        for (let i = 0; i < done_elements.length; i++) {
            done_elements[i].onclick=function () {
                let parent=this.parentNode;
                console.log(parent)
                if (done_elements[i].checked == true) {
                    target=document.querySelector('.done');
                }else {
                    target=document.querySelector('.todo');
                }
                console.log(target)
                target.appendChild(parent);
            }
        }
            

    }



    
   
</script>
</html>